package com.room.search.service;

import com.room.search.core.page.TableDataInfo;
import com.room.search.domain.RoomSupportingFacilities;
import com.room.search.repository.RoomSupportingFacilitiesRepository;
import com.room.search.repository.search.RoomSupportingFacilitiesSearchRepository;
import com.room.search.service.dto.RoomSupportingFacilitiesDTO;
import com.room.search.service.mapper.RoomSupportingFacilitiesMapper;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import com.room.search.util.PageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service Implementation for managing {@link RoomSupportingFacilities}.
 */
@Service
@Transactional
public class RoomSupportingFacilitiesService {

    private final Logger log = LoggerFactory.getLogger(RoomSupportingFacilitiesService.class);

    private final RoomSupportingFacilitiesRepository roomSupportingFacilitiesRepository;
    private final RoomSupportingFacilitiesMapper roomSupportingFacilitiesMapper;

    public RoomSupportingFacilitiesService(
        RoomSupportingFacilitiesRepository roomSupportingFacilitiesRepository,
        RoomSupportingFacilitiesMapper roomSupportingFacilitiesMapper) {
        this.roomSupportingFacilitiesRepository = roomSupportingFacilitiesRepository;
        this.roomSupportingFacilitiesMapper = roomSupportingFacilitiesMapper;
    }

    /**
     * Save a roomSupportingFacilities.
     *
     * @param roomSupportingFacilitiesDTO the entity to save.
     * @return the persisted entity.
     */
    @Transactional
    public RoomSupportingFacilities save(RoomSupportingFacilitiesDTO roomSupportingFacilitiesDTO) {
        log.debug("Request to save RoomSupportingFacilities : {}", roomSupportingFacilitiesDTO);
        RoomSupportingFacilities roomSupportingFacilities = roomSupportingFacilitiesMapper.toEntity(roomSupportingFacilitiesDTO);
        return roomSupportingFacilitiesRepository.save(roomSupportingFacilities);
    }

    /**
     * Partially update a roomSupportingFacilities.
     *
     * @param roomSupportingFacilitiesDTO the entity to update partially.
     * @return the persisted entity.
     */
    public Optional<RoomSupportingFacilitiesDTO> partialUpdate(RoomSupportingFacilitiesDTO roomSupportingFacilitiesDTO) {
        log.debug("Request to partially update RoomSupportingFacilities : {}", roomSupportingFacilitiesDTO);

        return roomSupportingFacilitiesRepository
            .findById(roomSupportingFacilitiesDTO.getId())
            .map(existingRoomSupportingFacilities -> {
                roomSupportingFacilitiesMapper.partialUpdate(existingRoomSupportingFacilities, roomSupportingFacilitiesDTO);

                return existingRoomSupportingFacilities;
            })
            .map(roomSupportingFacilitiesRepository::save)
            .map(roomSupportingFacilitiesMapper::toDto);
    }

    /**
     * Get all the roomSupportingFacilities.
     *
     * @return the list of entities.
     */
    @Transactional(readOnly = true)
    public List<RoomSupportingFacilitiesDTO> findAll() {
        log.debug("Request to get all RoomSupportingFacilities");
        return roomSupportingFacilitiesRepository
            .findAll()
            .stream()
            .map(roomSupportingFacilitiesMapper::toDto)
            .collect(Collectors.toCollection(LinkedList::new));
    }

    /**
     * Get one roomSupportingFacilities by id.
     *
     * @param id the id of the entity.
     * @return the entity.
     */
    @Transactional(readOnly = true)
    public Optional<RoomSupportingFacilitiesDTO> findOne(Long id) {
        log.debug("Request to get RoomSupportingFacilities : {}", id);
        return roomSupportingFacilitiesRepository.findById(id).map(roomSupportingFacilitiesMapper::toDto);
    }

    /**
     * Delete the roomSupportingFacilities by id.
     *
     * @param id the id of the entity.
     */
    public void delete(Long id) {
        log.debug("Request to delete RoomSupportingFacilities : {}", id);
        roomSupportingFacilitiesRepository.deleteById(id);
    }

    /**
     * 配套设施分页查询全部
     */
    public TableDataInfo getAllSupporting(RoomSupportingFacilitiesDTO dto) {
        Pageable pageable = PageUtils.startPage();
        Page<RoomSupportingFacilities> all = roomSupportingFacilitiesRepository.findAll(pageable);
        return TableDataInfo.getTableDataInfo(all);
    }
}
